<?php

// amespace Library\ErrorMsgClass;
class ErrorMsg {

    public static function Debug($msg = '', $success = false, $url = false) {
        if (XML_R == true) {
            self::Json($msg, $success, $url);
        } else {
            self::Html($msg, $success, $url);
        }
    }

    public static function Html($msg, $success = false, $url = false) {
        $HtmlTxt = '<!DOCTYPE html>';
        $HtmlTxt .= '<html lang="zh-CN">';
        $HtmlTxt .= '<head>';
        $HtmlTxt .= '<meta charset="utf-8">';
        $HtmlTxt .= '<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> ';
        $HtmlTxt .= '<title>系统提示</title>';
        $HtmlTxt .= '<style type="text/css" media="screen">';
        $HtmlTxt .= '*{margin: 0px;padding: 0px;}';
        $HtmlTxt .= 'p{margin: 15px;color:#585050;font:normal 0.9em \'微软雅黑\';}';
        $HtmlTxt .= '</style>';
        $HtmlTxt .= '</head>';
        $HtmlTxt .= '<body onkeydown="if(event.keyCode==27) return false;">';
        $HtmlTxt .= '<div>';
        $HtmlTxt .= '	<div style="width:94%;margin:0px auto;text-align:center;margin-top:50px;">';
        /* $HtmlTxt .= '		<img src="" width="260" />'; */
        $HtmlTxt .= '	</div>';
        $HtmlTxt .= '	<div style="text-align:center;border:1px solid #c7dd71;background:#effeb9;padding:10px;width:50%;margin:0 auto;">';

        if (!empty($url)) {
            $HtmlTxt.='<script>setTimeout("window.location.href=\'' . $url . '\'",5000);</script>';
        } elseif (!empty($_SERVER['HTTP_REFERER'])) {
            $HtmlTxt.='<script>setTimeout("window.location.href=\'' . $_SERVER['HTTP_REFERER'] . '\'",5000);</script>';
        }
        $HtmlTxt .= '		<p>' . $msg . '</p>';

        $HtmlTxt .= '	</div>';
        $HtmlTxt .= '</div>';
        $HtmlTxt .= '</body>';
        $HtmlTxt .= '</html>';
        $Htmlzip = array();
        $Config = Config::GetConfig();
        if (!empty($Config['rewrite'])) {
            $Htmlzip['/\/index\.php\//si'] = '/';
        }
        if (!empty($Config['htmlzip'])) {
            $Htmlzip['/>[\s\r\n\t]+/'] = '>';
            $Htmlzip['/[\s\r\n\t]+</'] = '<';
            $Htmlzip['/([\r\n\t]|[\s]{2,})+/'] = ' ';
        }
        $HtmlTxt = preg_replace(array_keys($Htmlzip), array_values($Htmlzip), $HtmlTxt);
        die($HtmlTxt);
    }

    public static function Json($msg, $success = false, $url = false) {
        $JsonTxt = array(
            'msg' => $msg,
            'success' => $success,
            'url' => $url
        );
        $Config = Config::GetConfig();
        $Htmlzip = array();
        if (!empty($Config['rewrite'])) {
            $Htmlzip['/\/index\.php\//si'] = '/';
        }
        if (!empty($Config['htmlzip'])) {
            $Htmlzip['/>[\s\r\n\t]+/'] = '>';
            $Htmlzip['/[\s\r\n\t]+</'] = '<';
            $Htmlzip['/([\r\n\t]|[\s]{2,})+/'] = ' ';
        }
        $JsonTxt = preg_replace(array_keys($Htmlzip), array_values($Htmlzip), json_encode($JsonTxt));
        die($JsonTxt);
    }

    public static function ApiMsg($msg = '', $errcode = '10000', $data = array(), $header = array('Content-type'=>"application/json")){
        $JsonTxt = array(
            'msg' => $msg,
            'errcode' => $errcode,
            'data' => $data
        );
        $Config = Config::GetConfig();
        $Htmlzip = array();
        if (!empty($Config['rewrite'])) {
            $Htmlzip['/\/index\.php\//si'] = '/';
        }
        if (!empty($Config['htmlzip'])) {
            $Htmlzip['/>[\s\r\n\t]+/'] = '>';
            $Htmlzip['/[\s\r\n\t]+</'] = '<';
            $Htmlzip['/([\r\n\t]|[\s]{2,})+/'] = ' ';
        }
//        $JsonTxt = preg_replace(array_keys($Htmlzip), array_values($Htmlzip), json_encode($JsonTxt)); // 输出的汉字不能直观显示
        foreach ($header as $k => $v){
            header($k.":".$v);
        }
//        header("Content-type: application/json");
        $JsonTxt = preg_replace(array_keys($Htmlzip), array_values($Htmlzip), \wodrow\tools\Tools::decodeUnicode(json_encode($JsonTxt)));
        die($JsonTxt);
    }
}